package com.qk.controller;

import com.qk.anno.Log;
import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.User;
import com.qk.service.UserService;
import dto.UserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 获取用户列表
     */
    @GetMapping
    public Result list(UserDto userDto) {
        log.info("用户列表查询");
        PageResult<User> pageResult = userService.getUsers(userDto);
        return Result.success(pageResult);


    }

    /**
     * 新增用户
     */
    @Log
    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户: {}", user);
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable List<Integer> id) {
        log.info("删除用户: {}", id);
        userService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询: {}", id);
        return Result.success(userService.getById(id));
    }

    /**
     * 修改用户
     */
    @Log
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 根据角色查询 用户
     */
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        log.info("根据角色查询用户: {}", roleLabel);
        List<User> users = userService.listByRole1(roleLabel);
        return Result.success(users);

    }
    /**
     * 查询全部用户
     *
     */
    @GetMapping("/list")
    public Result listAll() {
        log.info("查询全部用户");
        List<User> users = userService.listAll();
        return Result.success(users);
    }
    /**
     * 根据部门查询用户
     */
    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        log.info("根据部门查询用户: {}", deptId);
        List<User> users = userService.listByDept(deptId);
        return Result.success(users);
    }






    }

